/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.imagepipeline.bitmaps;

import com.facebook.common.memory.PooledByteBuffer;
import com.facebook.common.references.CloseableReference;
import com.facebook.common.webp.BitmapCreator;
import com.facebook.imageformat.DefaultImageFormats;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.memory.FlexByteArrayPool;
import com.facebook.imagepipeline.memory.PoolFactory;
import ohos.agp.utils.Color;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.AlphaType;
import ohos.media.image.common.PixelFormat;

/**
 * This is the implementation of the BitmapCreator for the Honeycomb
 */
public class HoneycombBitmapCreator implements BitmapCreator {

    private final EmptyJpegGenerator mJpegGenerator;
    private final FlexByteArrayPool mFlexByteArrayPool;

    public HoneycombBitmapCreator(PoolFactory poolFactory) {
        mFlexByteArrayPool = poolFactory.getFlexByteArrayPool();
        mJpegGenerator = new EmptyJpegGenerator(poolFactory.getPooledByteBufferFactory());
    }

    @Override
    public PixelMap createNakedBitmap(int width, int height, PixelFormat bitmapConfig) {
        CloseableReference<PooledByteBuffer> jpgRef =
                mJpegGenerator.generate((short) width, (short) height);
        EncodedImage encodedImage = null;
        CloseableReference<byte[]> encodedBytesArrayRef = null;
        try {
            encodedImage = new EncodedImage(jpgRef);
            encodedImage.setImageFormat(DefaultImageFormats.JPEG);
            ImageSource.DecodingOptions options = getBitmapFactoryOptions(encodedImage.getSampleSize(), bitmapConfig);

            int length = jpgRef.get().size();
            final PooledByteBuffer pooledByteBuffer = jpgRef.get();
            encodedBytesArrayRef = mFlexByteArrayPool.get(length + 2);
            byte[] encodedBytesArray = encodedBytesArrayRef.get();
            pooledByteBuffer.read(0, encodedBytesArray, 0, length);
            ImageSource imageSource = ImageSource.create(encodedBytesArray, 0, length,new ImageSource.SourceOptions());
            PixelMap bitmap = imageSource.createPixelmap(options);
  /*    bitmap.setHasAlpha(true);
      bitmap.eraseColor(Color.TRANSPARENT);*/
            bitmap.setAlphaType(AlphaType.UNKNOWN);
            //bitmapRef.get().eraseColor(Color.TRANSPARENT);
            bitmap.writePixels(0x00000000);
            return bitmap;
        } finally {
            CloseableReference.closeSafely(encodedBytesArrayRef);
            EncodedImage.closeSafely(encodedImage);
            CloseableReference.closeSafely(jpgRef);
        }
    }

    private static ImageSource.DecodingOptions getBitmapFactoryOptions(
            int sampleSize, PixelFormat bitmapConfig) {
        ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
        //options.inDither = true; // known to improve picture quality at low cost
        options.desiredPixelFormat = bitmapConfig;
        // Decode the image into a 'purgeable' bitmap that lives on the ashmem heap
        //options.inPurgeable = true;
        // Enable copy of of bitmap to enable purgeable decoding by filedescriptor
        //options.inInputShareable = true;
        // Sample size should ONLY be different than 1 when downsampling is enabled in the pipeline
        options.sampleSize = sampleSize;
        options.editable = true; // no known perf difference; allows postprocessing to work
        return options;
    }
}
